part of YFree;

const _yFreePlugin = MethodChannel('YFreePlugin');
_YFreeAppPlugin? _yFreeAppPlugin = (defaultTargetPlatform == TargetPlatform.android || defaultTargetPlatform == TargetPlatform.iOS) ? _YFreeAppPlugin() : null;
_YFreeWebPlugin? _yFreeWebPlugin = kIsWeb ? _YFreeWebPlugin() : null;
_YFreePCPlugin? _yFreePCPlugin = (defaultTargetPlatform == TargetPlatform.windows || defaultTargetPlatform == TargetPlatform.macOS || defaultTargetPlatform == TargetPlatform.linux) ? _YFreePCPlugin() : null;
final yIsApp = (_yFreeAppPlugin != null);
final yIsWeb = (_yFreeWebPlugin != null);
final yIsPC = (_yFreePCPlugin != null);

_YFreeCommonPlugin? _getPlugin() {
  if (yIsApp) return _yFreeAppPlugin;
  if (yIsWeb) return _yFreeWebPlugin;
  if (yIsPC) return _yFreePCPlugin;
  return null;
}

void yDoCrash(dynamic message) {
  if (!YConfig.catchCrash) return;
  yLog("\n$message");
  _getPlugin()!.doCrash(message.toString());
}

///检测更新
void yCheckUpdate({
  required int newVersion,
  required String updateContent,
  required String? apkDownloadPath,
  required String? androidAppId,
  required String? iOSAppId,
  bool enableIgnore = false,
  onNewest()?,
}) {
  _getPlugin()!.checkUpdate(newVersion: newVersion, updateContent: updateContent, apkDownloadPath: apkDownloadPath, androidAppId: androidAppId, iOSAppId: iOSAppId, enableIgnore: enableIgnore, onNewest: onNewest);
}

///退出App
void yFinishApplication() {
  _getPlugin()!.finishApplication();
}

///打开进度条，传入page可拦截安卓返回键
void yShowProgress() {
  _getPlugin()!.showProgress();
}

///关闭进度条，传入page可拦截安卓返回键
void yDismissProgress() {
  _getPlugin()!.dismissProgress();
}

///打印log
void yLog(String message) {
  _getPlugin()!.log(message);
}

///底部文字提示
void yToast(String message) {
  _getPlugin()!.toast(message);
}

///配置原生框架post地址
void yConfigIP(String ip) {
  YConfig.IP = ip;
  _getPlugin()!.configIP(YConfig.IP);
}

///配置原生框架检测升级地址
void yConfigUpdateIP(String updateIp) {
  YConfig.UPDATEIP = updateIp;
  _getPlugin()!.configUpdateIP(YConfig.UPDATEIP);
}

///主线程运行
void yRun(action(), {delay = 0}) {
  _getPlugin()!.runOnUIThread(delay, action);
}

///主线程运行(循环)
Timer yRunLoop(action(Timer? timer), int loop, {runNow = false}) {
  if (runNow) action(null);
  return Timer.periodic(Duration(milliseconds: loop), (timer) => action(timer));
}

///异步转同步
Future<T> yRunCompleter<T>(action(Completer<T> completer)) {
  final completer = Completer<T>();
  action(completer);
  return completer.future;
}

///获取App名称
String yGetAppName() {
  return _getPlugin()!.getAppName();
}

///base64转码
String yBase64Encode(String content) {
  return _getPlugin()!.base64_Encode(content);
}

///base64解码
String yBase64Decode(String content) {
  return _getPlugin()!.base64_Decode(content);
}

///aes加密
String yAesEncode(String content, String password) {
  return _getPlugin()!.aesEncode(content, password);
}

///aes解密
String yAesDecode(String content, String password) {
  return _getPlugin()!.aesDecode(content, password);
}

///设备唯一码
Future<String> yGetDeviceID() async {
  return _getPlugin()!.getDeviceID();
}

///请求存储权限
Future<bool> yRequestStorage({bool showFail = true}) async {
  return _getPlugin()!.requestStorage();
}

///检查网络是否可用
Future<bool> yIsNetAble() async {
  return _getPlugin()!.isNetAble();
}

///本地存值
Future<void> ySave(String key, String value, {bool secure = false}) async {
  await _getPlugin()!.save(key, value, secure: secure);
}

///本地取值
Future<String?> yRead(String key, {bool secure = false}) async {
  return _getPlugin()!.read(key, secure: secure);
}

///本地删值
Future<void> yDelete(String key, {bool secure = false}) async {
  _getPlugin()!.delete(key, secure: secure);
}

///复制到剪贴板
void yCopyClipBoard(String content) {
  _getPlugin()!.copyClipBoard(content);
}
